<!-- 添加企业组件 -->
<template>
  <div class="add-enterprise">
    <header class="add-header">
      <div class="left">
        <span class="arrow" @click="$router.back()"><i class="el-icon-arrow-left" />返回</span>
        <span>|</span>
        <span>添加企业</span>
      </div>
      <div class="right">
        黑马程序员
      </div>
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">企业信息</div>
        <div class="form">
          <el-form ref="ruleForm" label-width="100px" :model="addForm" :rules="rules">
            <el-form-item label="企业名称" prop="name">
              <el-input v-model="addForm.name" placeholder="请输入企业名称" />
            </el-form-item>
            <el-form-item label="法人" prop="legalPerson">
              <el-input v-model="addForm.legalPerson" placeholder="请输入法人" />
            </el-form-item>
            <el-form-item label="注册地址" prop="registeredAddress">
              <el-input v-model="addForm.registeredAddress" placeholder="请输入注册地址" />
            </el-form-item>
            <el-form-item label="所在行业" prop="industryCode">
              <el-select v-model="addForm.industryCode" placeholder="请选择所在行业">
                <el-option v-for="item in list" :key="item.industryCode" :value="item.industryCode" :label="item.industryName" />
              </el-select>
            </el-form-item>
            <el-form-item label="企业联系人" prop="contact">
              <el-input v-model="addForm.contact" placeholder="请输入企业联系人" />
            </el-form-item>
            <el-form-item label="联系电话" prop="contactNumber">
              <el-input v-model="addForm.contactNumber" placeholder="请输入联系电话" />
            </el-form-item>
            <el-form-item label="营业执照" prop="businessLicenseId">
              <el-upload
                ref="upload"
                action="#"
                :http-request="uploadRequest"
                :before-upload="beforeUpload"
                :file-list="[]"
              >
                <el-button icon="el-icon-upload2" size="small" style="color:#4770ff; background:#edf5ff" color="#4770ff">点击上传</el-button>
              </el-upload>
              <div>支持扩展名: .png .jpg .jpeg,文件大小不得超过5M</div>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button @click="resetBtn">重置</el-button>
        <el-button type="primary" @click="confirm">确定</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
import { addParkAPI, editParkAPI, getIndustryListAPI, getParkDetailAPI, uploadAPI } from '@/api/park'
export default {
  name: 'AddEnterprise',
  data() {
    return {
      addForm: {
        name: '', // 企业名称
        legalPerson: '', // 法人
        registeredAddress: '', // 注册地址
        industryCode: '', // 所在行业
        contact: '', // 企业联系人
        contactNumber: '', // 联系人电话
        businessLicenseUrl: '', // 营业执照url
        businessLicenseId: '' // 营业执照id
      },

      rules: {
        name: [
          { required: true, message: '请输入企业名称', trigger: 'blur' }
        ],
        legalPerson: [
          { required: true, message: '请输入法人', trigger: 'blur' }
        ],
        registeredAddress: [
          { required: true, message: '请输入注册地址', trigger: 'blur' }
        ],
        industryCode: [
          { required: true, message: '请选择所在行业', trigger: 'blur' }
        ],
        contact: [
          { required: true, message: '请输入企业联系人', trigger: 'blur' }
        ],
        contactNumber: [
          { required: true, message: '请输入联系电话', trigger: 'blur' },
          { pattern: /^[1][3,4,5,6,7,8,9][0-9]{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ]

      },
      list: [] // 行业列表
    }
  },
  created() {
    this.getIndustryList()
    if (this.$route.params.id) {
      console.log('id', this.$route.params.id)
      this.getCompanyData(this.$route.params.id)
    }
  },
  methods: {
    // 获取编辑数据
    async getCompanyData(id) {
      const res = await getParkDetailAPI(id)
      this.addForm = res.data
      // console.log(res)
    },

    // 确定按钮
    async confirm() {
      // 校验整个表单
      await this.$refs.ruleForm.validate()
      if (this.$route.params.id) {
        // 编辑
        console.log({ ...this.addForm, id: this.$route.params.id })
        const obj = { ...this.addForm, id: this.$route.params.id }
        // 删除一个属性
        delete obj.businessLicenseName
        delete obj.industryName
        delete obj.rent
        await editParkAPI(obj)
        //  提醒用户
        this.$message({ type: 'success', message: '修改成功' })
      } else {
        // 新增添加
        // 发送请求
        const res = await addParkAPI(this.addForm)
        console.log(res)
        // 提示用户添加成功
        this.$message({ type: 'success', message: '添加成功' })
        // 跳转回到列表页面
        this.$router.push('/park/enterprise')
      }
    },
    // 重置按钮
    resetBtn() {
      // 重置表单
      this.$refs.ruleForm.resetFields()
      // 清空文件上传列表
      this.$refs.upload.clearFiles()
    },
    // 校验图片
    beforeUpload(file) {
      console.log('执行了吗？', file)
      const allowImgType = ['image/jpeg', 'image/png', 'image/jpg']
      // 图片格式校验
      // 图片大小要小于5M
      const isLt5M = file.size / 1024 / 1024 < 5
      // 图片类型校验
      const flag = allowImgType.includes(file.type)
      if (!flag) {
        this.$message.error('上传合同图片只能是 PNG/JPG/JPEG 格式!')
      }
      if (!isLt5M) {
        this.$message.error('图片大小不能超过5M')
      }
      return flag && isLt5M
    },
    // 上传图片
    async uploadRequest(file) {
      // console.log('校验结果', this.beforeUpload())
      console.log(file)
      // formData 使用于 上传文件 和图片
      const formData = new FormData()
      formData.append('file', file.file)
      formData.append('type', 'businessLicense')
      // console.log(formData)
      // 调用上传接口
      const { data: { id, url }} = await uploadAPI(formData)
      this.addForm.businessLicenseId = id
      this.addForm.businessLicenseUrl = url
    },

    // 获取企业所属行业的列表
    async getIndustryList() {
      const { data } = await getIndustryListAPI()
      this.list = data
    }
  }
}
</script>

<style scoped lang="scss">
.add-enterprise {
  background-color: #f4f6f8;
  height: 100vh;

  .add-header {
    display: flex;
    justify-content: space-between;
    padding: 0 20px;
    height: 64px;
    line-height: 64px;
    font-size: 16px;
    font-weight: bold;
    background-color: #fff;

    .left {
      span {
        margin-right: 4px;
      }

      .arrow {
        cursor: pointer;
      }
    }

    .right {
      text-align: right;
    }
  }

  .add-main {
    background: #f4f6f8;
    padding: 20px 130px;

    .form-container {
      background-color: #fff;

      .title {
        height: 60px;
        line-height: 60px;
        padding-left: 20px;
      }

      .form {
        margin-bottom: 20px;
        padding: 20px 65px 24px;

        .el-form {
          display: flex;
          flex-wrap: wrap;

          .el-form-item {
            width: 50%;
          }
        }
      }
    }
  }

  .add-footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    padding: 24px 50px;
    color: #000000d9;
    font-size: 14px;
    background: #fff;
    text-align: center;
  }
}
</style>
